#include "CF.h"
#include "CF_SceneNode.h"
#include "CF_WorkspaceManager.h"
#include "AppMacros.h"

USING_NS_CC;
USING_NS_CC_EXT;

CF_SceneNode::CF_SceneNode(const QString& name, const QString& fileName)
{
	m_name = name;
	if(fileName.isEmpty())
		m_fileName = m_name + "." CF_SCENE_FILE_EXT;
	else
		m_fileName = fileName;
}

CF_SceneNode::~CF_SceneNode()
{
}

void CF_SceneNode::restore()
{
	m_scene = CCScene::create();
	
    Point origin = Director::getInstance()->getVisibleOrigin();
    Size visibleSize = Director::getInstance()->getVisibleSize();
	LabelTTF* label = LabelTTF::create(m_name.toStdString().c_str(), "Arial", TITLE_FONT_SIZE);
    
    // position the label on the center of the screen
    label->setPosition(Point(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    m_scene->addChild(label, 1);

	ControlButton* button = ControlButton::create("Click", "Arial", TITLE_FONT_SIZE);
	button->setPosition(100, 100);
	m_scene->addChild(button);
}

QString CF_SceneNode::makeSceneFilePath() const
{
	return workspaceManager->getProjectPath() + "/" + m_fileName;
}

void CF_SceneNode::setAsCurrentScene()
{
	if( Director::getInstance()->getRunningScene() )
		Director::getInstance()->replaceScene(getScene());
	else
		Director::getInstance()->runWithScene(getScene());
}

bool CF_SceneNode::save()
{
	QFile file(makeSceneFilePath());
	file.open(QIODevice::WriteOnly | QIODevice::Text);
	file.close();

	return true;
}
